//
//  ForumMainViewViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-12.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "HaveReadOrdersViewController.h"

#import "ForumMainViewViewController.h"

static ASIHTTPConnection *haveReadOrders_connect;


@interface HaveReadOrdersViewController (){
    
    int pageNo;
}

@end

@implementation HaveReadOrdersViewController

@synthesize myTableView;

- (void)dealloc{
    
    if (haveReadOrders_connect != nil) {
        haveReadOrders_connect.connectionDelegate = nil;
        [haveReadOrders_connect release];
        haveReadOrders_connect = nil;
    }
    
    [myArray  release];
    [myTableView release];
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.tabBarItem.image = [UIImage imageNamed:@"icon_history.png"];
        self.tabBarItem.title = @"历史医嘱";
        self.title = @"历史医嘱";

    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    
    pageNo = 1;
        
    [super viewWillAppear:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   // [self refreshView];
    
    [self setTItleWith:@"已处理医嘱"];
    
    [self loadData];
    
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    
    if (myArray == nil) {
        myArray = [[NSMutableArray alloc]init];
    }
    
    pageNo = 1;
    
    
    // Do any additional setup after loading the view from its nib.
}


- (void)loadData{
    
    
    NSString *UserID = [[NSUserDefaults standardUserDefaults] valueForKey:USER_ID];
    
    NSString *companyName = [[NSUserDefaults standardUserDefaults] valueForKey:COMPANYNAME];
    
    NSString *urlString = [NSString stringWithFormat:@"%@/MobileHealth/CommunityUserListAction.aspx?CompanyName=%@",BASEURL,companyName];
	NSURL *url = [NSURL URLWithString:urlString];
    
    
    if (haveReadOrders_connect != nil) {
        haveReadOrders_connect.connectionDelegate = nil;
        [haveReadOrders_connect release];
        haveReadOrders_connect = nil;
    }
    
    haveReadOrders_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    haveReadOrders_connect.connectionDelegate = self;
    
    [haveReadOrders_connect startAsynchronous];
    
    
}



#pragma mark connectino delegate
- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    CLog(@"orders list = %@",dict);
        
    
    if (myArray != nil) {
        [myArray removeAllObjects];
    }
    
    
    NSArray *array = [dict objectForKey:@"datalist"];
    
    if (array != nil && array.count > 0) {
        
        if (myArray == nil) {
            myArray = [[NSMutableArray alloc]init];
        }
        
        [myArray addObjectsFromArray:array];
        [myTableView reloadData];
       
    }
    
    
    
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    [self  headerRefDone];
    [self bottomRefDone];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return myArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MyIdentifier = @"forumCell";
    
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] init] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
	   
        
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        
        cell.backgroundColor = [UIColor clearColor];
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView *bgview = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 310,140 )];
        bgview.image = [[UIImage imageNamed:@"writ bg_staggerer_iPhone.png"] stretchableImageWithLeftCapWidth:100 topCapHeight:20];
        [cell.contentView addSubview:bgview];
        [bgview release];

        
        UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];
        iconImageView.tag = 1;
        [cell.contentView addSubview:iconImageView];
        [iconImageView release];
        
        UILabel *namelable = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 150, 30)];
        namelable.backgroundColor = [UIColor clearColor];
        namelable.textColor = [UIColor blackColor];
        namelable.textAlignment = UITextAlignmentLeft;
        namelable.font = [UIFont boldSystemFontOfSize:15];
        [cell.contentView addSubview:namelable];
        namelable.tag = 2;
        [namelable release];
    }
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    
    UIImageView *iconImageView = (UIImageView *)[cell viewWithTag:1];
    iconImageView.image = [UIImage imageNamed:@"geren_set_iphone.png"];
    
    UILabel *nameLable = (UILabel *)[cell viewWithTag:2];
      
    nameLable.text = [dict objectForKey:@"UserName"];
    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    NSString *userId = [dict objectForKey:@"UserID"];
    ForumMainViewViewController *viewCon = [[ForumMainViewViewController alloc]initWithUserID:userId];
    viewCon.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewCon animated:YES];
    [viewCon release];
    
}



#pragma mark -
#pragma mark Table 下拉刷新
#pragma mark -
#pragma mark Data Source Loading / Reloading Methods


- (void)refreshView
{
    
    //顶部刷新条
    if ( _refreshHeaderView == nil) {
        
        _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f- self.myTableView .bounds.size.height, 320, self.myTableView.bounds.size.height)];
        _refreshHeaderView.delegate = self;
        [self.myTableView addSubview:_refreshHeaderView];
    }
    
    
    //底部刷新条
    if ( _refreshTailView == nil) {
        
        _refreshTailView = [[EGORefreshTableTailView alloc] initWithFrame:CGRectMake(0.0f, self.myTableView.contentSize.height, 320, self.myTableView.frame.size.height)];
        _refreshTailView.delegate = self;
        [self.myTableView addSubview:_refreshTailView];
        
    }
    
    if (_refreshTailView != nil) {
        float height = MAX(self.myTableView.contentSize.height, self.myTableView.frame.size.height);
        [_refreshTailView setFrame:CGRectMake(0.0f, height, 320, self.myTableView.frame.size.height)];
    }
    
    
}



//顶部刷新事件
- (void)headerRefData
{
    _HeaderReloading = YES;
    pageNo = 1;
    [self loadData];
    
}

- (void)headerRefDone
{
    _HeaderReloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.myTableView];
}

//底部刷新事件
- (void)bottomRefData
{
    
    
    _TailReloading = YES;
    
    pageNo = myArray.count/20 +1;
    
    [self loadData];
    
}

- (void)bottomRefDone
{
    _TailReloading = NO;
    if (_refreshTailView != nil) {
        
        [_refreshTailView egoRefreshScrollViewDataSourceDidFinishedLoading:self.myTableView];
    }
    
}



#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
    //上拉
    if (_refreshTailView != nil) {
        
        [_refreshTailView egoRefreshScrollViewDidScroll:scrollView];
    }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	
    
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    //上拉
    if (_refreshTailView != nil) {
        
        [_refreshTailView egoRefreshScrollViewDidEndDragging:scrollView];
    }
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}


#pragma mark -
#pragma mark EGORefreshTableTailDelegate Methods

- (void)egoRefreshTableTailDidTriggerRefresh:(EGORefreshTableTailView*)view{
	
	[self bottomRefData];
    
}

- (BOOL)egoRefreshTableTailDataSourceIsLoading:(EGORefreshTableTailView*)view{
	
    
    return _TailReloading;
	
}

- (NSDate*)egoRefreshTableTailDataSourceLastUpdated:(EGORefreshTableTailView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView *)view{
    
	[self headerRefData];
	
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView *)view{
	
	//return _reloading; // should return if data source model is reloading
    return _HeaderReloading;
	
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView *)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}



@end
